package org.groupg.practice.guiPractice.tools;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class AWTGraphicsExample extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        // 设置抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);

        // 绘制基本形状
        g2d.setColor(Color.RED);
        g2d.fillRect(50, 50, 100, 100); // 矩形

        g2d.setColor(Color.BLUE);
        g2d.fillOval(200, 50, 100, 100); // 圆形

        g2d.setColor(Color.GREEN);
        g2d.fillRoundRect(350, 50, 100, 100, 20, 20); // 圆角矩形

        // 绘制线条和多边形
        g2d.setColor(Color.BLACK);
        g2d.setStroke(new BasicStroke(3)); // 设置线条粗细

        // 线条
        g2d.drawLine(50, 200, 150, 300);

        // 多边形
        int[] xPoints = {200, 250, 300, 250, 200};
        int[] yPoints = {200, 150, 200, 250, 300};
        g2d.drawPolygon(xPoints, yPoints, 5);

        // 使用Graphics2D高级特性
        GradientPaint gradient = new GradientPaint(350, 200, Color.RED,
                                                  450, 300, Color.YELLOW);
        g2d.setPaint(gradient);
        g2d.fill(new Ellipse2D.Double(350, 200, 100, 100));

        // 绘制文本
        g2d.setColor(Color.BLACK);
        g2d.setFont(new Font("Serif", Font.BOLD, 24));
        g2d.drawString("Java 2D 图形示例", 200, 400);

        // 变换操作
        g2d.rotate(Math.toRadians(45), 550, 400);
        g2d.setColor(Color.MAGENTA);
        g2d.fillRect(500, 350, 100, 100);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("AWT 绘图示例");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(800, 600);
            frame.add(new AWTGraphicsExample());
            frame.setVisible(true);
        });
    }
}
